package com.nanoso.crawl.spider;

import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.nanoso.crawl.concurrent.UrlsConsumer;
import com.nanoso.crawl.concurrent.UrlsProducer;
import com.nanoso.module.Url;
import com.nanoso.util.Switch;

public class Spider {
	Log log = LogFactory.getLog(Spider.class);
	private final BlockingQueue<Url> queue = new LinkedBlockingQueue<Url>();
	private UrlsProducer urlsProducer;
	private UrlsConsumer urlsConsumer;
	private static final int THREAD_POOL_SIZE = 10;
	private ExecutorService exec = Executors
			.newFixedThreadPool(THREAD_POOL_SIZE);
	private Switch _switch;

	public void setUrlsProducer(UrlsProducer urlsProducer) {
		this.urlsProducer = urlsProducer;
	}

	public void setUrlsConsumer(UrlsConsumer urlsConsumer) {
		this.urlsConsumer = urlsConsumer;
	}

	public void set_switch(Switch switch1) {
		_switch = switch1;
	}

	public void doSpider(List<Url> urls) {
		
		if (!_switch.isSpiderSwitch())
			return;
		if (urls.size() == 0)
			return;

		try {
			urlsProducer.setUrls(urls);
			urlsProducer.setQueue(queue);
			new Thread(urlsProducer).start();

			urlsConsumer.setQueue(queue);
			exec.execute(urlsConsumer);
		} finally {
			exec.shutdown();
		}
	}

}
